# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

import requests
import json
import logging
from requests.exceptions import ConnectionError
from scrapy.exceptions import IgnoreRequest

class CookiesMiddleware():

    def __init__(self,cookies_pool_url):
        self.cookies_pool_url=cookies_pool_url
        self.logger=logging.getLogger(__name__)
        self.cooikes={'_T_WM': '05a6077555155d19c63bb2d47f2ba866', ' ALF': '1526886869', ' SCF': 'AuY-pj-KR8C_pF4A91wzBAALY3T793th-kgh_DVOA3\
wU3OFGUyMiF-R7hZRYFAK8nxMUYqYedcS7fVLxxdfrUFY.', ' SUB': '_2A2533oCZDeRhGeNM6lMX9CjOwjWIHXVVICDRrDV6PUJbktAKLU3tkW1NTlV\
1DzNAWXybTZdaZjZ6hrlVjZCF0T-u', ' SUBP': '0033WrSXqPxfM725Ws9jqgMF55529P9D9W5I1P0H9lV-2R2oHzkPqvEe5JpX5K-hUgL.Fo-EeK2cS\
hqE1K.2dJLoI7LKdg8jd2fD9s.t', ' SUHB': '0h1JavAX-yy624', ' SSOLoginState': '1524297929'}
    
    def get_random_cookies(self):
        try:
            response=requests.get(self.cookies_pool_url)
            if response.status_code==200:
                return json.loads(response.text)
        except ConnectionError:
            return None
    
    @classmethod
    def from_crawler(cls,crawler):
        return cls(
            cookies_pool_url=crawler.settings.get('COOKIES_POOL_URL')
    )

    def process_request(self,request,spider):
        cookies=self.cooikes
        if cookies:
            request.cookies=cookies
            self.logger.debug('Using Cookies'+json.dumps(cookies))
        else:
            self.logger.debug('No Valid Cookies')

    def process_response(self,request,response,spider):

        if response.status in [300,301,302,303]:
            try:
                redirect_url=response.headers('localtion')
                if 'passport' in redirect_url:
                    self.logger.warning('need login,update cookie')
                elif 'weibo.cn/security' in redirect_url:
                    self.logger.warning('account is locked!!')
                request.cooikes=self.get_random_cookies()
                return request
            except :
                raise IgnoreRequest
        elif response.status in [414]:
            return request
        else:
            return response       